package mystia.lorelei.app.service;

import jakarta.annotation.Resource;
import mystia.lorelei.app.entity.User;
import mystia.lorelei.app.repository.UserRepository;
import mystia.lorelei.app.util.Encrypt;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Resource
    private UserRepository userRepository;

    public User getUserByUsername(String username) {
        return userRepository.findByUsername(username);
    }
    public List<User> listUsers() {
        return userRepository.findAll();
    }

    public boolean login(User user) {
        User userInDatabase = userRepository.findByUsername(user.getUsername());
        if (userInDatabase == null) {
            return false;
        }
        String inputPassword = Encrypt.sha512(user.getPassword());
        return userInDatabase.getPassword().equals(inputPassword);
    }

    public void saveUser(User user) {
        userRepository.save(user);
    }
    
}

